/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package adalid.core;

import adalid.core.data.types.ByteData;
import adalid.core.data.types.CharacterData;
import adalid.core.data.types.IntegerData;
import adalid.core.data.types.ShortData;
import adalid.core.data.types.StringData;
import adalid.core.interfaces.PersistentEntity;
import adalid.core.interfaces.Property;
import adalid.core.primitives.BooleanPrimitive;
import adalid.core.primitives.NumericPrimitive;
import adalid.core.primitives.TemporalPrimitive;
import adalid.core.sql.QueryTable;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public class ReportField extends AbstractArtifact implements Comparable<ReportField> {

    private ReportGroup _group;

    private Property _property;

    private Property _parentProperty;

    private int _pixels;

    private boolean _resizeable;

    public ReportField(ReportGroup group, String name) {
        if (group != null && name != null) {
            _group = group;
            setDeclared(name);
            add();
        }
    }

    public ReportField(ReportGroup group, Property property) {
        if (group != null && property != null) {
            _group = group;
            _property = property;
            _pixels = pixels(property);
            _resizeable = _pixels > 120;
            Report report = group.getReport();
            PersistentEntity entity = report == null ? null : report.getEntity();
            QueryTable queryTable = entity == null ? null : entity.getQueryTable();
            String sqlAlias = queryTable == null ? null : queryTable.getSqlAlias(property);
            if (sqlAlias != null) {
                setDeclared(sqlAlias);
                add();
            }
        }
    }

    /*
     when('varchar')then
     * (case
     * when(att.atttypmod<0)then(384)
     * when((att.atttypmod-4)>100)then(256)
     * when((att.atttypmod-4)>50)then(192)
     * when((att.atttypmod-4)>20)then(128)
     * else(48)
     * end)
     */
    private int pixels(Property property) {
        if (property == null || !property.isReportField()) {
            return 0;
        } else if (property instanceof BooleanPrimitive) {
            return 48;
        } else if (property instanceof CharacterData) {
            return 48;
        } else if (property instanceof StringData) {
            StringData string = (StringData) property;
            Integer maxLength = string.getMaxLength();
            if (maxLength == null) {
                return 384;
            } else if (maxLength > 100) {
                return 256;
            } else if (maxLength > 50) {
                return 192;
            } else if (maxLength > 20) {
                return 128;
            } else if (maxLength > 10) {
                return 64;
            } else {
                return 48;
            }
        } else if (property instanceof ByteData || property instanceof ShortData || property instanceof IntegerData) {
            return 64;
        } else if (property instanceof NumericPrimitive) {
            return 120;
        } else if (property instanceof TemporalPrimitive) {
            return 64;
        } else {
            return 0;
        }
    }

    private void add() {
        _group.getFields().add(this);
    }

    /**
     * @return the report group
     */
    public ReportGroup getReportGroup() {
        return _group;
    }

    /**
     * @return the property
     */
    public Property getProperty() {
        return _property;
    }

    /**
     * @return the parent property
     */
    public Property getParentProperty() {
        return _parentProperty;
    }

    void setParentProperty(Property parent) {
        _parentProperty = parent;
    }

    /**
     * @return the pixels
     */
    public int getPixels() {
        return _pixels;
    }

    /**
     * @param pixels the pixels to set
     */
    void setPixels(int pixels) {
        _pixels = pixels;
    }

    /**
     * @return the resizeable
     */
    public boolean isResizeable() {
        return _resizeable;
    }

    /**
     * @param resizeable the resizeable to set
     */
    void setResizeable(boolean resizeable) {
        _resizeable = resizeable;
    }

    // <editor-fold defaultstate="collapsed" desc="Comparable">
    @Override
    public int compareTo(ReportField o) {
        ReportField that;
        if (o != null) {
            that = o;
            String thisName = StringUtils.trimToEmpty(this.getName());
            String thatName = StringUtils.trimToEmpty(that.getName());
            return thisName.compareTo(thatName);
        }
        return 0;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="toString">
    @Override
    protected String fieldsToString(int n, String key, boolean verbose, boolean fields, boolean maps) {
        String tab = verbose ? StringUtils.repeat(" ", 4) : "";
        String fee = verbose ? StringUtils.repeat(tab, n) : "";
        String faa = " = ";
        String foo = verbose ? EOL : ", ";
        String string = super.fieldsToString(n, key, verbose, fields, maps);
        if (fields || verbose) {
            if (verbose) {
                string += fee + tab + "pixels" + faa + _pixels + foo;
                string += fee + tab + "resizeable" + faa + _resizeable + foo;
            }
        }
        return string;
    }
    // </editor-fold>

}
